﻿<%dim respagecode%><% 'UTF-8%>

<% '资源管理%>
<%''加载通用函数和数据库连接%>
<!--#include file="../Include/Start.asp"-->
<!--#include file="../Include/check_Permissions_do.asp"-->

<%

	'定义json对象
	Dim jsonObj,jsonObj_user
	Set jsonObj = jsObject()
	Set jsonObj_user = jsObject()
	
	UserID=ReplaceBadChar(Trim(Session("UserID")))
	
	dim act 
  act=request("act")
 	Dim rsObj,strSql,strSqlTemp
 	set rsObj=Server.CreateObject("ADODB.Recordset")
 	''定义查询字段变量
 	dim id, res_name, res_num, menu_pname, menu_name, menu_code, menu_num,  res_do, res_url, res_ppath, res_role,menu_show
	''查询信息列表
	if "search"=act then
		resnum=ReplaceBadChar(Trim(request.Form("resnum")))
		resname=ReplaceBadChar(Trim(request.Form("resname")))
		res_url=ReplaceUrlBadChar(Trim(request.Form("resurl")))
		dim page ''当前查询页
		page=ReplaceBadChar(Trim(request.Form("page")))&""
				
		jsonObj("rslist") = ""
		''添加用户信息
		jsonObj_user("UserID") = UserID 
		jsonObj_user("UserName") =ReplaceBadChar(Trim(Session("UserName")))
		Set jsonObj("customer") = jsonObj_user 
	 
		''添加查询结果信息，按照菜单排序
		''Dim rsObj,strSql 
		strSql = "SELECT top 100 PERCENT id, res_name, res_num, menu_pname, menu_name, menu_code, menu_num,  res_do, res_url, res_ppath  "
		strSql=strSql & " ,res_ppath + CAST(id AS VARCHAR(10)) + '_' AS res_path  "
		strSql=strSql & " ,res_ppath + CAST(id AS VARCHAR(10)) + '_' AS orderid1  "
		strSql=strSql & "  FROM T_RES "   & "WHERE 1=1 "
		If (resnum <> "") Then strSql=strSql & " and res_num like '%" & resnum & "%'  "
		If (resname <> "") Then strSql=strSql & " and res_name like '%" & resname & "%'  "
 		If (res_url <> "") Then strSql=strSql & " and res_url like '%" & res_url & "%'  "
		strSql=strSql & " ORDER BY orderid1  "
		
		dim TotalRsNum ,PageSizeCur''TotalRsNum总记录数''PageSizeCur当前页记录数
		strSqlTemp = "select count(*)  from ( " & strSql & " ) kksds "
		TotalRsNum = PE_CLng(Conn.Execute(strSqlTemp)(0))
		''PageSize 页的记录数量
		PageSizeCur = PageSize ''当前页记录数
		if (PageSize*PE_CLng(page)>TotalRsNum or 0=TotalRsNum) then ''判断最后一页记录数是否和PageSize相等
			PageSizeCur= TotalRsNum Mod PageSize ''该页的时间记录数
		end if

		strSqlTemp = " SELECT ttkkk.* FROM (SELECT TOP " & CStr(PageSizeCur)
		strSqlTemp = strSqlTemp & "  mmsss.* FROM (SELECT TOP " & CStr(PageSize*PE_CLng(page)) & " nnfff.* FROM ( " 
		strSqlTemp = strSqlTemp &	strSql  
		strSqlTemp = strSqlTemp &  " ) nnfff) mmsss  ORDER BY mmsss.orderid1 DESC) ttkkk ORDER BY ttkkk.orderid1"
		Set jsonObj("rslist") = QueryToJSON(Conn, strSqlTemp)
		
		''添加分页信息
		'定义json对象
		Dim jsonObj_page
		Set jsonObj_page = jsObject()
			jsonObj_page("PageCur") = Page ''当前页	
			jsonObj_page("PageSizeCur") = PageSizeCur ''当前页记录数
			if (0=TotalRsNum  mod PageSize  ) then
				jsonObj_page("PageTotalNum")= TotalRsNum\PageSize''总页数
			else
				jsonObj_page("PageTotalNum")= TotalRsNum\PageSize+1''总页数
			end if
		Set jsonObj("page") = jsonObj_page 
	 
	
 	elseif "add"=act then
		jsonObj("msg") = "&#x64CD;&#x4F5C;&#x5931;&#x8D25;&#xFF0C;&#x8BF7;&#x91CD;&#x8BD5;..."'''' "操作失败，请重试..."
		jsonObj("success") = 0'"false"
		respagecode="res_add"
		if not(check_Permissions(UserID,respagecode)) then 
			jsonObj("msg") = "权限受限，操作失败!"
			jsonObj("success") = 0'"false"
			jsonObj("url") = "../passport/nopermission.asp"
		else''添加信息
			id=ReplaceBadChar(Trim(request.Form("id")))
			res_name=ReplaceBadChar(Trim(request.Form("res_name")))
			res_num=ReplaceBadChar(Trim(request.Form("res_num")))
			menu_pname=ReplaceBadChar(Trim(request.Form("menu_pname")))
			menu_name=ReplaceBadChar(Trim(request.Form("menu_name")))
			menu_code=ReplaceBadChar(Trim(request.Form("menu_code")))			
			menu_num=ReplaceBadChar(Trim(request.Form("menu_num")))			
			res_do=ReplaceBadChar(Trim(request.Form("res_do")))			
			res_url=ReplaceUrlBadChar(Trim(request.Form("res_url")))			
			res_ppath=ReplaceBadChar(Trim(request.Form("res_ppath")))			
 
 
			''Dim rsObj,strSql
			strSql = "Select  * from T_RES where id='"&id&"' "
			''Response.Write strSql&"-----"
			rsObj.Open strSql, Conn, 1, 3
			If rsObj.BOF And rsObj.EOF Then 
				rsObj.AddNew '如果没记录新增，有记录update	
				strSql = "select ISNULL(MAX(id), 0)  from T_RES  "
				id = PE_CLng(Conn.Execute(strSql)(0)) + 1
			end if				
			''rsObj("id") = PE_CLng(id)
			rsObj("res_name") = res_name
			rsObj("res_num") = res_num''PE_CLng(res_num)
			rsObj("menu_pname") = menu_pname
			rsObj("menu_name") = menu_name
			rsObj("menu_code") = menu_code
			rsObj("menu_num") = PE_CLng(menu_num)
			rsObj("res_do") = res_do
			rsObj("res_url") = res_url
			rsObj("res_ppath") = res_ppath
			rsObj.Update
			rsObj.close
			''Set rsObj = Nothing
		 
			jsonObj("msg") = "添加成功，正在跳转..."
			jsonObj("success") = 1'"success"
			jsonObj("url") = "resources.asp"
	 	 end if''权限判断end
 elseif "edit"=act then
		jsonObj("msg") = "操作失败，请重试..."
		jsonObj("success") = 0'"false"
		respagecode="res_edit"
		if not(check_Permissions(UserID,respagecode)) then 
			jsonObj("msg") = "权限受限，操作失败!"
			jsonObj("success") = 0'"false"
			jsonObj("url") = "../passport/nopermission.asp"			
		else''修改信息

			id=ReplaceBadChar(Trim(request.Form("id")))
			res_name=ReplaceBadChar(Trim(request.Form("res_name")))
			res_num=ReplaceBadChar(Trim(request.Form("res_num")))
			menu_pname=ReplaceBadChar(Trim(request.Form("menu_pname")))
			menu_name=ReplaceBadChar(Trim(request.Form("menu_name")))
			menu_code=ReplaceBadChar(Trim(request.Form("menu_code")))			
			menu_num=ReplaceBadChar(Trim(request.Form("menu_num")))			
			res_do=ReplaceBadChar(Trim(request.Form("res_do")))			
			res_url=ReplaceUrlBadChar(Trim(request.Form("res_url")))			
			res_ppath=ReplaceBadChar(Trim(request.Form("res_ppath")))		
			menu_show=ReplaceBadChar(Trim(request.Form("menu_show")))		
			
			res_role=ReplaceBadChar(Trim(request.Form("res_role"))) ''res对应的role列表

 
			''Dim rsObj,strSql
			''set rsObj=Server.CreateObject("ADODB.Recordset")
			strSql = "Select  * from T_RES where id='"&id&"' "
			''Response.Write strSql&"-----"
			rsObj.Open strSql, Conn, 1, 3
			If rsObj.BOF And rsObj.EOF Then 
				jsonObj("msg") =  "操作失败，请重试..."
				jsonObj("success") = 0'"false"
					rsObj.close
			else				
				''rsObj("id") = PE_CLng(id)
				rsObj("res_name") = res_name
				rsObj("res_num") = res_num''PE_CLng(res_num)
				rsObj("menu_pname") = menu_pname
				rsObj("menu_name") = menu_name
				rsObj("menu_code") = menu_code
				rsObj("menu_num") = PE_CLng(menu_num)
				rsObj("res_do") = res_do
				rsObj("res_url") = res_url
				rsObj("res_ppath") = res_ppath
				rsObj("menu_show") = menu_show
				rsObj.Update
					rsObj.close
			 	
			 	''删除该资源原有的角色，添加现有已经选中的角色
			  RowCount = 0
		    Conn.Execute ("delete from T_ROLE_RES where res_id='"&id&"'"), RowCount		    	
		    ''添加选中的角色
				strSql =" INSERT INTO T_ROLE_RES ( res_id,role_id) "
				strSql =strSql & " SELECT  "&id&" AS res_role_id ,id"
				strSql =strSql & " FROM T_ROLE WHERE 1=2 "
				If ("" <> res_role ) Then  strSql =strSql & " or id IN ("&res_role&")  "
		    Conn.Execute (strSql), RowCount		 

				jsonObj("msg") =  "添加成功，正在跳转..."
				jsonObj("success") = 1'"success"
				jsonObj("url") = "resources.asp"
	 		end if	
		end if''权限判断end
 
 elseif "delete"=act then
 		respagecode="res_del"
		if not(check_Permissions(UserID,respagecode)) then 
			jsonObj("msg") = "权限受限，操作失败!"
			jsonObj("success") = 0'"false"
			jsonObj("url") = "../passport/nopermission.asp"
		else''删除记录
		    Dim  RowCount 
			id=ReplaceBadChar(Trim(request.Form("id"))) 
			RowCount = 0
		    strSql = "select count(id)  from T_ROLE_RES where  res_id ='"&id&"'"
			RowCount = PE_CLng(Conn.Execute(strSql)(0)) 
			if 0<RowCount then 
				jsonObj("success") = 0 'false
		        jsonObj("msg") = "记录被引用，删除失败."  
			else
			    RowCount = 0
			    If (id <> "" ) Then 
			    	Conn.Execute ("delete from T_RES where id='"&id&"'"), RowCount
			    End If
			    If RowCount = 0 Then
			        jsonObj("success") = 0 'false
			        jsonObj("msg") = "记录删除失败。" 
			    Else
			        jsonObj("msg") = "删除成功，正在跳转..." 
							jsonObj("success") = 1'"success"
							jsonObj("url") = "resources.asp"
			    End If
		    End If''删除记录
  		end if''权限判断end

	end If''end all do
 			
 	Set rsObj = Nothing


	'输出json对象
	jsonObj.Flush
	''Response.Write strSql&"-----"
 '释放数据库链接
	call CloseConn

''{
''    rslist : [ { name: "mac", desc: "computer",     
''                   price: 1000, quantity: 100, alert:null },
''                 { name: "ipod", desc: "music player", 
''                   price:  200, quantity: 200, alert:"on sale now!" },
''                 { name: "cinema display", desc: "screen",       
''                   price:  800, quantity: 300, alert:"best deal!" } ],
''    customer : { first: "John", last: "Public", level: "gold" }
''}

%>